package com.efounder.service;

import android.util.Log;

import com.core.xml.StubObject;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.service.util.RegistryUtil;

import org.w3c.dom.Element;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Registry {
    private static final String TAG = "Registry";

    public static Map<String, List<StubObject>> registryStore = new HashMap<String, List<StubObject>>();
    public static Map<String, StubObject> registryNodeStore = new HashMap<String, StubObject>();

    /**
     * 初始化配置文件
     *
     * @param dirName package文件目录
     * @return 是否初始化成功
     */
    public static boolean init(String dirName) {
        clearRegistry();
        File dir = new File(dirName);
        if (dir == null || !dir.exists()) {
            Log.e(TAG, "配置文件所在的文件夹不存在!dirName=" + dirName);
            return false;
        }
        if (dir.isDirectory()) {
            EnvironmentVariable.setProperty(Registry.class.getName() + ":dirName", dirName);
            File[] files = dir.listFiles();
            if (files == null || files.length == 0) {
                Log.e(TAG, "配置文件所在的目录不存在任何文件!dirName=" + dirName);
                return false;
            }

            //重新初始化时，我们的fmt配置也需要重新初始化
            releaseFmt();
            for (File file : files) {
                if (file.getName().toLowerCase().endsWith(".xml")) {
                    registerXmlFile(file.getAbsolutePath());
                }
            }
        } else {
            Log.e(TAG, "传入的路径不是文件夹：" + dirName);
            return false;
        }
        return true;
    }

    /**
     * 注册xml配置文件
     *
     * @param fileName
     */
    private static void registerXmlFile(String fileName) {
        RegistryUtil.initRegistry(fileName);
    }

    /**
     * 重新初始化时，我们的fmt配置也需要重新初始化
     */
    private static void releaseFmt() {
        try {
            RegistryUtil.getFmtFormatUil().method("release");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放清除缓存
     */
    public static void clearRegistry() {
        registryStore.clear();
        registryNodeStore.clear();
    }

    public static StubObject getRegEntry(String entryKey) {
        if (registryNodeStore.size() == 0) {//如果被回收，重新初始化
            init(EnvironmentVariable.getProperty(Registry.class.getName() + ":dirName"));
        }
        return registryNodeStore.get(entryKey);
    }

    public static List<StubObject> getRegEntryList(String entryKey) {
        if (registryStore.size() == 0) {//如果被回收，重新初始化
            init(EnvironmentVariable.getProperty(Registry.class.getName() + ":dirName"));
        }
        return registryStore.get(entryKey);
    }

    public static void regStubObjectList(String entryKey, StubObject stub) {
        List<StubObject> regList = registryStore.get(entryKey);
        if (regList == null) {
            regList = new ArrayList();
            registryStore.put(entryKey, regList);
        }
        regList.add(stub);
    }

    public static void regStubObject(String entryKey, StubObject stub) {
        registryNodeStore.put(entryKey, stub);
    }


    //fmt相关
    public static List<Element> getFmtElement() {
        List<Element> elementList = initFmtDir(EnvironmentVariable.getProperty(Registry.class.getName() + ":dirName"));
        return elementList;
    }

    public static List<Element> initFmtDir(String dirName) {
        List<Element> elementList = new ArrayList<>();
        File dir = new File(dirName);
        if (!dir.isDirectory()) {
            return elementList;
        }

        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.getName().toLowerCase().endsWith(".xml")) {
                Element element = RegistryUtil.parseFmtXml(file.getAbsolutePath());
                if (element != null) {
                    elementList.add(element);
                }
            }
        }
        return elementList;
    }

}
